package advancedpart.assignmentA2;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import shared.*;

import java.util.HashMap;

public class SearchCmdA2 {

    public static void main (String[] args) throws IOException {		
		if (args.length !=1) {
			System.out.println("Usage: java <datafile>");
			System.exit(1);
		}
		
    	//Variable for input
        String inputString;
        
        //Variable for input splited by " "
        String[] splitInput;
        
        //Dataset
        HashMap<String, WordList> wordMap = SearcherA2.createWordMap(args[0]);

        // Ask for a word to search
        BufferedReader inuser =
            new BufferedReader (new InputStreamReader (System.in));

        System.out.println ("Hit return to exit.");
        boolean quit = false;
        while (!quit) {
        	// Inform user of options
            System.out.println ("Search for two words using 'and' or 'or' as operator");
            System.out.println ("Enter search:");
            
            // Read a line from the terminal
            inputString = inuser.readLine(); 
                        
            if (inputString == null || inputString.length() == 0) { //Check if return has been hit to exit.
                quit = true;
            } else {            	
                //Split the input into an array of words. 
                splitInput = inputString.split(" ");
                               
                if(splitInput.length == 3){ //Check if there is precisely 3 words
                	//Create variables for the words 
                	String firstWord = splitInput[0].toLowerCase();
                	String secondWord = splitInput[2].toLowerCase();
                	
            		//Create enum for search operator; either AND or OR
            		SearchOperator operator;
            		if(splitInput[1].equalsIgnoreCase("and")){
            			operator = SearchOperator.AND; 
            		} else if (splitInput[1].equalsIgnoreCase("or")){
            			operator = SearchOperator.OR; 
            		} else { //if the operator is not valid
            			operator = null;
            		}
                	
                	//Check if the operator is valid
                	if(operator != null){
                		//Check if either of the words are in the data set
                		boolean firstWordExists = SearcherA2.exists(wordMap, firstWord); 
                		boolean secondWordExists = SearcherA2.exists(wordMap, secondWord); 
                		
                		//Create String to output
                		String outputString = "No pages found";
                		
                		if(firstWordExists && secondWordExists){//If both words exists
                			//Use method for searching for two words
                			outputString = SearcherA2.getUrlsForTwoWords(wordMap, firstWord, secondWord, operator);
                		
                		} else if(firstWordExists && !secondWordExists){//If only the first word exists
                			//If or operator is used search for the word that is present
                			if(operator == SearchOperator.OR){
                				outputString = SearcherA2.getUrlsForOneWord(wordMap, firstWord);
                			}               				  
                			
                		} else if(!firstWordExists && secondWordExists){//if only the second word exist
                			//If or operator is used search for the word that is present
                			if(operator == SearchOperator.OR){
                				outputString = SearcherA2.getUrlsForOneWord(wordMap, secondWord);
                			}
                		 } else {//If none of the words exists -> do nothing
                			
                		}
                		//Print result
                		System.out.println(outputString);
                	
                	} else {//If the operator is not "and" or "or" prompt for proper input
                		System.out.println("Please use correct operator");
                	
                	}            
                	
                } else { //If there is not exactly three words prompt the user for proper input
                	System.out.println("Please submit corret input");
                
                }
            }
        }
    }
}